import { createApp } from 'vue'
import App from './App.vue'
//引入路由
import Router from '@/routers/index';
//引入Vant UI框架
//  引入全部组件
import  Vant  from 'vant';
// 引入组件样式
import 'vant/lib/index.css';

//引入状态管理
import { createPinia } from 'pinia';

//创建状态管理,挂载
const pinia = createPinia()
//引入让状态管理保持持久的插件
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
//引入自定义状态
import Stores from '@/stores/Stores'; 

// 不需要登录验证路由规则
import Rules from '@/routers/rules';

// 引入自定义函数
import Tools from '@/tools/tools';

//引入封装请求服务
import request from '@/services/request';




const app = createApp(App)
.use(Router)
.use(Vant)
.use(pinia.use(piniaPluginPersistedstate))
.use(Stores)
.use(Tools)
.use(request)
.mount('#app');

Router.beforeEach(async (to,from) => {
    let path = to.path ?? '';

    if(Rules.includes(path))
    {
        return true;
    }

    let business = app.$business.value ? app.$business.value : {};
    if(Object.getOwnPropertyNames(business).length === 0)
        {
            // 提示信息
            app.$warning('请先登录');
    
            // 重定向登录页面
            return {path:'/business/login'};
        }
        let id = business.id ?? 0;
        let mobile = business.mobile ?? '';

        let result = await app.$http.post({
            url:'/business/check',
            params:{
                id,
                mobile
            }
        })
        if(result.code === 1)
        {
            app.$business.value = result.data;
            return true;
        }else{
            app.$warning(result.msg);

            app.$ResetBusiness();
            return {path:'/business/login'};


        }
        

        
});
